package org.surelink.radius.api.thrift;


import org.apache.thrift.TException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.surelink.radius.cache.ClientCache;
import org.surelink.radius.cache.GroupCache;
import org.surelink.radius.cache.OnlineCache;
import org.surelink.radius.cache.UserCache;
import org.surelink.radius.model.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApiServiceImpl implements ApiService.Iface
{

    @Autowired
    private ClientCache clientCache;

    @Autowired
    private OnlineCache onlineCache;

    @Autowired
    private GroupCache groupCache;

    @Autowired
    private UserCache userCache;

    @Override
    public void setClient(TClient client) throws TException
    {
        Client rclient = new Client();
        BeanUtils.copyProperties(client,rclient);
        clientCache.set(rclient);
    }

    @Override
    public TClient getClient(String ipAddr) throws TException
    {
        Client rclient = clientCache.get(ipAddr);
        TClient tclient = new TClient();
        BeanUtils.copyProperties(rclient,tclient);
        return tclient;
    }

    @Override
    public void delClient(String ipAddr) throws TException
    {
        clientCache.del(ipAddr);
    }

    @Override
    public void setOnline(TOnline online) throws TException
    {
        Online ritem = new Online();
        BeanUtils.copyProperties(online,ritem);
        onlineCache.set(ritem);
    }

    @Override
    public TOnline getOnline(String clientAddr, String sessionId) throws TException
    {
        Online ritem = onlineCache.get(clientAddr,sessionId);
        TOnline titem = new TOnline();
        BeanUtils.copyProperties(ritem,titem);
        return titem;
    }

    @Override
    public void delOnline(String clientAddr, String sessionId) throws TException
    {
        onlineCache.del(clientAddr,sessionId);
    }

    @Override
    public List<TOnline> getOnlineList(int tstart, int tend) throws TException
    {
        List<Online> ronlines = onlineCache.getOnlineList(tstart,tend);
        List<TOnline> tonlines = new ArrayList<TOnline>();
        for (Online online : ronlines)
        {
            TOnline titem = new TOnline();
            BeanUtils.copyProperties(ronlines,titem);
            tonlines.add(titem);

        }
        return tonlines;
    }

    @Override
    public void setGroup(TGroup group) throws TException
    {
        Group ritem = new Group();
        BeanUtils.copyProperties(group,ritem);
        groupCache.set(ritem);
    }

    @Override
    public TGroup getGroup(String groupName) throws TException
    {
        Group ritem = groupCache.get(groupName);
        TGroup titem = new TGroup();
        BeanUtils.copyProperties(ritem,titem);
        return titem;
    }

    @Override
    public void delGroup(String groupName) throws TException
    {
        groupCache.del(groupName);
    }

    @Override
    public String getGroupMeta(String groupName, String metaName) throws TException
    {
        return groupCache.getGroupMeta(groupName,metaName);
    }

    @Override
    public void setGroupMeta(String groupName, String metaName, String metaValue) throws TException
    {
        groupCache.setGroupMeta(groupName,metaName,metaValue);
    }

    @Override
    public void setUser(TUser user) throws TException
    {
        User ritem = new User();
        BeanUtils.copyProperties(user,ritem);
        userCache.set(ritem);
    }

    @Override
    public TUser getUser(String userName) throws TException
    {
        User ritem = userCache.get(userName);
        TUser titem = new TUser();
        BeanUtils.copyProperties(ritem,titem);
        return titem;
    }

    @Override
    public void delUser(String userName) throws TException
    {
        userCache.del(userName);
    }

    @Override
    public String getUserMeta(String userName, String metaName) throws TException
    {
        return userCache.getUserMeta(userName,metaName);
    }

    @Override
    public void setUserMeta(String userName, String metaName, String metaValue) throws TException
    {
        userCache.setUserMeta(userName,metaName,metaValue);
    }
}
